package xingkong.core;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

import com.alibaba.druid.util.StringUtils;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import xingkong.tools.ResponseContent;
import xingkong.tools.StaticFields;
/**
 * 订单
 * @author befory
 *
 */
public class OrderController extends Controller{
	/**
	 * 到订单列表
	 */
	public void toOrders(){
		renderJsp("/WEB-INF/content/orders.jsp");
	}
	/**
	 * 提交订单
	 * @throws Exception 
	 */
	public void addOrder() throws Exception{
		int type = getParaToInt("type");//订单类型
		if(type <1 || type >5){
			renderJson(new ResponseContent(StaticFields.WRONG, "类型选择错误", null));
			return;
		}
		int num = getParaToInt("num");
		if(num <1){
			renderJson(new ResponseContent(StaticFields.WRONG, "数量选择错误", null));
			return;
		}
		String link = getPara("link");
		if(StringUtils.isEmpty(link)){
			renderJson(new ResponseContent(StaticFields.WRONG, "链接不能为空", null));
			return;
		}
		String regex = "^([hH][tT]{2}[pP]:/*|[hH][tT]{2}[pP][sS]:/*|[fF][tT][pP]:/*)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+(\\?{0,1}(([A-Za-z0-9-~]+\\={0,1})([A-Za-z0-9-~]*)\\&{0,1})*)$";
		if(!Pattern.matches(regex, link)){
			renderJson(new ResponseContent(StaticFields.WRONG, "链接格式不对", null));
			return;
		}
		int integralTotal = getParaToInt("integral");
		if(integralTotal <1){
			renderJson(new ResponseContent(StaticFields.WRONG, "积分总数不对", null));
			return;
		}
		int price = Db.queryInt("select price from tb_business where type=?",type);
		int integral = num * price;
		if(integralTotal != integral){
			renderJson(new ResponseContent(StaticFields.WRONG, "积分总数不对", null));
			return;
		}
		String common_text = getPara("zidingyi");
		if(3==type){
			if(StringUtils.isEmpty(common_text)){
				renderJson(new ResponseContent(StaticFields.WRONG, "自定义评论不能为空", null));
				return;
			}
			if(common_text.length() > 200){
				renderJson(new ResponseContent(StaticFields.WRONG, "自定义评论200字以内", null));
				return;
			}
			common_text=common_text.toLowerCase();
			for (int i = 0,length = StaticFields.QUOTES.length; i < length; i++) {
				if(common_text.indexOf(StaticFields.QUOTES[i]) > 0){
					renderJson(new ResponseContent(StaticFields.WRONG, "评论里面包含非法字符", null));
					return;
				}
			}
			
		}
		Record user = (Record) getSession().getAttribute(StaticFields.CURRENTUSER);
		Record account = Db.findFirst("SELECT `id`,`integral` FROM `xingkong`.`tb_integral` i where i.user_id=?",user.getStr("id"));
		int myintegral = 0;
		if(null != account){
			myintegral = account.get("integral");
		}
		if(myintegral < integralTotal){
			renderJson(new ResponseContent(StaticFields.WRONG, "账户积分小于订单积分，无法支付", null));
			return;
		}
		
		
		Record record = new Record().set("link", link).set("user_id", user.get("id")).set("create_time", new Date()).set("type", type).set("order_code", UUID.randomUUID().toString());
		record.set("integral_total", integralTotal);
		switch (type) {
		case 1:
			record.set("praise_num", num).set("attention_num", 0).set("commen_num", 0).set("share_num", 0).set("play_num", 0);//视频点赞
			break;
		case 2:
			record.set("attention_num", num).set("praise_num", 0).set("commen_num", 0).set("share_num", 0).set("play_num", 0);//关注
			break;
		case 3:
			record.set("commen_num", num).set("praise_num", 0).set("attention_num", 0).set("share_num", 0).set("play_num", 0).set("common_text", common_text);//评论
			break;
		case 4:
			record.set("share_num", num).set("praise_num", 0).set("attention_num", 0).set("commen_num", 0).set("play_num", 0);//分享
			break;
		case 5:
			record.set("play_num", num).set("praise_num", 0).set("attention_num", 0).set("commen_num", 0).set("share_num", 0);//播放
			break;

		default:
			break;
		}
		boolean f = Db.save("tb_order", record);
		if(!f){
			renderJson(new ResponseContent(StaticFields.WRONG, "添加数据异常", null));
			throw new Exception();
		}
		int i = Db.update("update tb_integral set integral = integral -? where user_id = ? and integral >= ?",integralTotal,user.getStr("id"),integralTotal);
		if(i<1){
			renderJson(new ResponseContent(StaticFields.WRONG, "订单异常", null));
			throw new Exception();
		}
		renderJson(new ResponseContent(StaticFields.CORRECT, "添加数据成功", null));

	}
	
	/**
	 * 订单列表
	 */
	public void getOrders(){
		int page = getParaToInt("page");//当前页码
		if(page ==0) page =1;
		int size = getParaToInt("size");
		if(size ==0) size = StaticFields.DEFAULT_SIZE;
		Record record = (Record) getSession().getAttribute(StaticFields.CURRENTUSER);
		List<Record> list = null;
		List<Record> relist = null;
		long count = 0L;
		long total=0L;
		if("1".equals(record.get("type"))){
			//管理员
			list = Db.find("select * from tb_order limit ?,?",(page-1)*size,size);
			count = Db.queryLong("select count(1) from tb_order");
		}else {
			list = Db.find("select * from tb_order where user_id = ? limit ?,?",record.getStr("id"),(page-1)*size,size);
			count = Db.queryLong("select count(1) from tb_order where user_id = ?",record.getStr("id"));
		}
		if(count == 0){
			total = 1;
		}else {
			total = count % size == 0 ? count / size : (count / size +1);
		}
		renderJson(new ResponseContent(StaticFields.CORRECT, "", new Record().set("page", page).set("size", size).set("count", count).set("list", list).set("total", total)));

	}
	
	/**
	 * 计算总积分
	 */
	public void countTotal(){
		String type = getPara("type");
		if(StringUtils.isEmpty(type)){
			renderJson(new ResponseContent(StaticFields.WRONG, "类型选择错误", null));
			return;
		}
		int num = getParaToInt("num");
		if(num <1){
			renderJson(new ResponseContent(StaticFields.WRONG, "数量错误", null));
			return;
		}
		Record record = Db.findFirst("select * from tb_business where type = ?",type);
		if(null ==  record){
			renderJson(new ResponseContent(StaticFields.WRONG, "暂时没有这样的业务", null));
			return;
		}
		int integral = record.getInt("price")*num;
		renderJson(new ResponseContent(StaticFields.CORRECT, "", integral));
	}
}
